#!/bin/bash

# 生成随机端口号
generate_port() {
  port=$((RANDOM % 65536))
  echo $port
}

# 验证端口号是否重复
is_port_duplicate() {
  local port=$1
  netstat -tuln | awk '{print $4}' | awk -F ":" '{print $NF}' | grep -wq $port
  return $?
}

# 生成并验证端口号
generate_and_check_port() {
  local port=$(generate_port)
  while is_port_duplicate $port; do
    port=$(generate_port)
  done
  echo "$port"
}

generate_and_check_port
